#include "debug_addmenu.h"
#include "ui_debug_addmenu.h"
#include "bookstore.h"
#include "QString"
#include <string>
#include <iostream>
#include <QMessageBox>
#include "utils.h"
using namespace utils;
using namespace bookstore;

debug_addMenu::debug_addMenu(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::debug_addMenu) {
    ui->setupUi(this);
}

debug_addMenu::~debug_addMenu() {
    delete ui;
}

void debug_addMenu::on_pushButton_clicked() {
    QString str = ui->lineEdit->text();
    string name = string((const char *)str.toLocal8Bit());
    if(name.size() == 0) {
        QMessageBox::warning(this, "warning", "请输入书籍名称");
        return;
    }
    else if(judgeSpace(name)==0)
    {
        QMessageBox::warning(this,"warning","请输入正确的书籍名称");
        return;
    }
    else if(name.length() > 96) {
        QMessageBox::warning(this, "warning", "书籍名称不能大于96字节");
        return;
    }

    else
        ;
    QString str2 = ui->lineEdit_2->text();
    string writer = string((const char *)str2.toLocal8Bit());
    if(writer.size() == 0) {
        QMessageBox::warning(this, "warning", "请输入作者名称");
        return;
    }
    else if(judgeSpace(writer)==0)
    {
        QMessageBox::warning(this,"warning","请输入正确的作者名称");
        return;
    }
    else if(writer.length() > 96) {
        QMessageBox::warning(this, "warning", "作者名称不能大于96字节");
        return;
    }

    else
        ;
    QString str3 = ui->lineEdit_3->text();
    string ISBN = string((const char *)str3.toLocal8Bit());
    if(ISBN.size() == 0) {
        //QMessageBox::warning(this, "warning", "请输入ISBN");
        //return;
    }

    else if(judge_menu(ISBN)==-1)
    {
        QMessageBox::warning(this,"warning","ISBN格式有误");
        return;
    }

    else
        ;
    QString str4 = ui->lineEdit_4->text();
    int count;
    if(isDigitStr(str4))
    {
        count = str4.toInt();
        if(count == 0) {
            QMessageBox::warning(this, "warning", "书籍数不能为0");
            return;
        }
            else if(count>100)
            {
                QMessageBox::warning(this,"warning","一次性加入书籍数小于100");
                return;
            }

        else
            ;
    }
    else
    {
        QMessageBox::warning(this,"warning","输入数字");
        return;
    }
    QString str5 = ui->lineEdit_5->text();
    string pub = string((const char *)str5.toLocal8Bit());
    if(pub.size()==0)
    {
        QMessageBox::warning(this,"warning","请输入出版社名称");
        return;
    }
    else if(pub.length()>96)
    {
        QMessageBox::warning(this,"warning","出版社名称不能大于96字节");
        return;
    }
    else if(judgeSpace(pub)==0)
    {
        QMessageBox::warning(this,"warning","请输入正确的出版社名称");
        return;
    }
    else
        ;
    Menu* menu = new Menu;
    menu = createMenu(name, writer, ISBN, pub);
    Menu* menusearch= search_onemenuReturn(ISBN, menuDatPath);
    Menu* temp = new Menu;
    memset(temp, 0, sizeof (Menu));
    if(memcmp(menusearch,temp,sizeof (Menu))==0)
    {
        addMenu(menu, count);
        delete menu;
         QMessageBox::information(this, "info", "添加成功");
         return;
    }
    else if(strcmp(menusearch->name,name.data())==0&&strcmp(menusearch->writer,writer.data())==0)
    {
        Book* book = new Book;
        book = createBook(menusearch->ID);
        addBook(book, count);
        delete book;
         QMessageBox::information(this, "info", "添加成功");
         return;
    }
    else{
        QMessageBox::warning(this,"warning","请检查信息是否正确");
        return;
    }


}
